package com.itheima.web.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.domain.vo.CommentVo;
import com.itheima.domain.vo.MovementVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.VideoVo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.MovementService;
import com.itheima.service.mongo.VideoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserManager {

    @Reference
    private UserInfoService userInfoService;

    @Reference
    private VideoService videoService;
    /**
     * 用户列表数据
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public PageBeanVo findUsers(Integer pageNumber, Integer pageSize) {
        //分页查询
        PageBeanVo pageBeanVo = userInfoService.findUsers(pageNumber, pageSize);
        return pageBeanVo;

    }

    /**
     * 查询用户详情
     * @param userId
     * @return
     */
    public UserInfo findUserById(Long userId) {
        return userInfoService.findById(userId);
    }

    /**
     * 分页查询视频列表
     */
    public PageBeanVo findVideoByUserId(Integer pageNumber, Integer pageSize, Long uid) {
        //1.调用分页方法查询数据
        PageBeanVo pageBeanVo = videoService.findVideoByUid(pageNumber, pageSize, uid);

        //2.构建videoVo对象
        List<Video> items = (List<Video>) pageBeanVo.getItems();
        List<VideoVo> videoVoList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(items)) {
            for (Video video : items) {
                VideoVo vo = new VideoVo();
                UserInfo userInfo = userInfoService.findById(video.getUserId());
                //转换
                BeanUtil.copyProperties(userInfo, vo);
                BeanUtil.copyProperties(video, vo);
                //赋值不一样的字段
                //封面
                vo.setCover(video.getPicUrl());
                //签名
                vo.setSignature(video.getText());
                videoVoList.add(vo);
            }
        }
        //赋值数据
        pageBeanVo.setItems(videoVoList);
        return pageBeanVo;
    }

    @Reference
    private MovementService movementService;
    /**
     *  根据传入的用户id和需要查询的动态状态 查询数据封装成vo对象
     */
    public PageBeanVo findMovementList(Integer pageNumber, Integer pageSize, Long uid, Integer stateInt) {
        //分页查询动态详情
        PageBeanVo pageBeanVo = movementService.findMovementsByUserIdAndState(pageNumber, pageSize, uid, stateInt);
        List<Movement> items = (List<Movement>) pageBeanVo.getItems();
        List<MovementVo> voList = new ArrayList<>();
        //转换
        if (CollectionUtil.isNotEmpty(items)) {
            for (Movement movement : items) {
                //查询用户的数据
                UserInfo userInfo = userInfoService.findById(movement.getUserId());
                //封装数据
                MovementVo vo = new MovementVo();
                vo.setUserInfo(userInfo);
                vo.setMovement(movement);
                //时间的转换
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date(movement.getCreated());//将时间戳转换成日期格式
                String format = simpleDateFormat.format(date);
                vo.setCreateDate(format);
                voList.add(vo);
            }
        }
        pageBeanVo.setItems(voList);
        return pageBeanVo;
    }

    /**
     * 查询动态详情
     * @param movementId
     * @return
     */
    public MovementVo findMovementById(String movementId) {
        System.out.println("接收到的movementId为：----" + movementId);
        Movement movement = movementService.findMovementById(movementId);
        MovementVo vo = new MovementVo();

        if (movement != null) {
            //查询用户数据
            UserInfo userInfo = userInfoService.findById(movement.getUserId());
            //封装数据
            vo.setUserInfo(userInfo);
            vo.setMovement(movement);
            //时间转换
            vo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(movement.getCreated())));
        }
        return vo;
    }

    @Reference
    private CommentService commentService;
    /**
     * 分页查询评论列表
     */
    public PageBeanVo findCommentList(Integer pageNumber, Integer pageSize, String movementId) {
        //查询评论列表数据
        PageBeanVo pageBeanVo = commentService.findMovementComment(pageNumber, pageSize, movementId);
        List<Comment> items = (List<Comment>) pageBeanVo.getItems();
        List<CommentVo> voList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(items)) {
            for (Comment comment : items) {
                //封装
                CommentVo vo = new CommentVo();
                //用户信息
                UserInfo userInfo = userInfoService.findById(comment.getUserId());
                vo.setId(comment.getId().toHexString());//评论id
                vo.setAvatar(userInfo.getAvatar());//头像
                vo.setNickname(userInfo.getNickname());//昵称
                vo.setContent(comment.getContent());//评论内容
                vo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(comment.getCreated())));//创建时间

                voList.add(vo);

            }
        }
        pageBeanVo.setItems(voList);
        return pageBeanVo;
    }

    /**
     * 动态审核是否通过
     * @param movementIds
     * @param option
     * @return
     */
    public Boolean auditMovementIfPass(List<String> movementIds, Boolean option) {
        Boolean flag = true;
        for (String movementId : movementIds) {

            //根据动态id和option操作类型进行审核操作
            flag = movementService.auditMovementIfPass(movementId, option);
        }


        return flag;
    }
}
